package com.prevpn.api;

import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import com.prevpn.common.CommonResponse;
import com.prevpn.common.CommonUtil;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * GroupApi - 调用 aTrust OpenAPI 组织架构接口
 */
@Component
public class GroupApi {

    /**
     * 调用 aTrust 新增组织架构接口
     */
    public CommonResponse<Map<String, Object>> createGroup(String directoryDomain,
                                                           String name,
                                                           String key,
                                                           String data,
                                                           String description,
                                                           String externalId) {
        String path = "/api/v3/group/create";

        Map<String, Object> body = new HashMap<>();
        body.put("directoryDomain", directoryDomain);
        body.put("name", name);

        Map<String, Object> groupObj = new HashMap<>();
        groupObj.put("key", key);
        groupObj.put("data", data);
        body.put("group", groupObj);

        if (description != null) body.put("description", description);
        if (externalId != null) body.put("externalId", externalId);

        String response = CommonUtil.openApiPostRequest(path, JSONUtil.toJsonStr(body));

        CommonResponse<Map<String, Object>> result = JSONUtil.toBean(
                response, new TypeReference<CommonResponse<Map<String, Object>>>() {}, false);
        return result;
    }

    /**
     * 查询组织架构详情（基于 fullPath）
     * GET /api/v3/group/queryByFullPath
     */
    public CommonResponse<Map<String, Object>> queryGroupByFullPath(String directoryDomain, String fullPath) {
        String path = "/api/v3/group/queryByFullPath";
        Map<String, Object> params = new HashMap<>();
        params.put("directoryDomain", directoryDomain);
        params.put("fullPath", fullPath);

        String response = CommonUtil.openApiGetRequest(path, params);
        return JSONUtil.toBean(response, new TypeReference<CommonResponse<Map<String, Object>>>() {}, false);
    }

    /**
     * 删除组织架构（支持多个 fullPath）
     * POST /api/v3/group/bulkDeleteByFullPathList
     */
    public CommonResponse<Map<String, Object>> deleteGroupByFullPathList(String directoryDomain, List<String> fullPathList) {
        String path = "/api/v3/group/bulkDeleteByFullPathList";

        Map<String, Object> body = new HashMap<>();
        body.put("directoryDomain", directoryDomain);
        body.put("fullPathList", fullPathList);

        String response = CommonUtil.openApiPostRequest(path, JSONUtil.toJsonStr(body));
        return JSONUtil.toBean(response, new TypeReference<CommonResponse<Map<String, Object>>>() {}, false);
    }

    /**
     * 调用 aTrust 编辑组织架构接口
     * 对应 /api/v3/group/updateByFullPath
     */
    public CommonResponse<Map<String, Object>> updateGroup(String directoryDomain,
                                                           String fullPath,
                                                           String name,
                                                           String description,
                                                           String externalId,
                                                           String groupKey,
                                                           String groupData) {
        String path = "/api/v3/group/updateByFullPath";

        Map<String, Object> body = new HashMap<>();
        body.put("directoryDomain", directoryDomain);
        body.put("fullPath", fullPath);

        if (name != null) body.put("name", name);
        if (description != null) body.put("description", description);
        if (externalId != null) body.put("externalId", externalId);

        // 组装上级组织对象（如果传了）
        if (groupKey != null && groupData != null) {
            Map<String, Object> groupObj = new HashMap<>();
            groupObj.put("key", groupKey);
            groupObj.put("data", groupData);
            body.put("group", groupObj);
        }

        String response = CommonUtil.openApiPostRequest(path, JSONUtil.toJsonStr(body));

        return JSONUtil.toBean(response,
                new TypeReference<CommonResponse<Map<String, Object>>>() {}, false);
    }

}
